package com.lonely.fx.table_view;

import com.lonely.fx.table_view.entity.Student;
import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.*;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.Callback;

/**
 * @date: 2022-08-02 21:35
 * @author: ztkj-hzb
 * @description: tableview 单元格编辑设置
 **/
public class TableViewCellFactorySettingMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {

        AnchorPane anchorPane = new AnchorPane();


        ObservableList<Student> persons = FXCollections.observableArrayList();
        persons.add(new Student("小天", "男", 0.25, false));
        persons.add(new Student("小地", "女", 0.5, Boolean.TRUE));
        persons.add(new Student("小玄", "男", 0.75, false));
        persons.add(new Student("小黄", "女", 1.0, true));

        TableView<Student> tableView = new TableView<>(persons);

        tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

        tableView.setPrefSize(400, 400);


        TableColumn<Student, String> nameTableCol = new TableColumn<>("学生名称");
        TableColumn<Student, String> sexTableCol = new TableColumn<>("性别");
        TableColumn<Student, Double> scoreTableCol = new TableColumn<>("成绩");
        TableColumn<Student, Boolean> passTableCol = new TableColumn<>("是否及格");

        nameTableCol.setCellValueFactory(new PropertyValueFactory<>("name"));
        sexTableCol.setCellValueFactory(new PropertyValueFactory<>("sex"));
        scoreTableCol.setCellValueFactory(new PropertyValueFactory<>("score"));
        passTableCol.setCellValueFactory(param -> param.getValue().getPassProperty());

        //passTableCol.setCellFactory(CheckBoxTableCell.forTableColumn(passTableCol));

        tableView.getColumns().addAll(nameTableCol, sexTableCol, scoreTableCol, passTableCol);
        anchorPane.getChildren().addAll(tableView);
        AnchorPane.setTopAnchor(tableView, 100.0);
        primaryStage.setScene(new Scene(anchorPane));
        primaryStage.setWidth(800);
        primaryStage.setHeight(700);
        primaryStage.show();


        //TODO 设置单元格样式
        //设置可编辑
        tableView.setEditable(true);

        //TextFieldTableCell 类型设置
        nameTableCol.setCellFactory(TextFieldTableCell.forTableColumn());

        //ComboBoxTableCell 类型设置
        sexTableCol.setCellFactory(ComboBoxTableCell.forTableColumn("男", "女"));
        //sexTableCol.setCellFactory(ChoiceBoxTableCell.forTableColumn("男", "女"));

        //ProgressBarTableCell 类型设置
        scoreTableCol.setCellFactory(ProgressBarTableCell.forTableColumn());

        //TODO 要想实现回显效果，1. pass需要是 SimpleBooleanProperty  2. 需要设置cellValueFactory返回可监听的类型
        passTableCol.setCellFactory(CheckBoxTableCell.forTableColumn(passTableCol));

    }
}
